package cn.iocoder.yudao.module.pay.framework.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ReflectUtil;
import cn.iocoder.yudao.module.api.pay.order.eums.UnifiedPayChannelEnum;
import cn.iocoder.yudao.module.pay.framework.PayClient;
import cn.iocoder.yudao.module.pay.framework.PayClientConfig;
import cn.iocoder.yudao.module.pay.framework.PayClientFactory;
import cn.iocoder.yudao.module.pay.framework.impl.alipay.*;
import cn.iocoder.yudao.module.pay.framework.impl.cash.CashPayClient;
import cn.iocoder.yudao.module.pay.framework.impl.free.FreePayClient;
import cn.iocoder.yudao.module.pay.framework.impl.mallbook.MallBookWxLitePayClient;
import cn.iocoder.yudao.module.pay.framework.impl.mock.MockPayClient;
import cn.iocoder.yudao.module.pay.framework.impl.wallet.WalletPayClient;
import cn.iocoder.yudao.module.pay.framework.impl.weixin.*;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static cn.iocoder.yudao.module.api.pay.order.eums.UnifiedPayChannelEnum.*;

/**
 * 支付客户端的工厂实现类
 *
 * @author 芋道源码
 */
@Slf4j
public class PayClientFactoryImpl implements PayClientFactory {
    /**
     * 支付客户端 Map
     * key：渠道编码
     */
    private final ConcurrentMap<String, AbstractPayClient<? extends PayClientConfig>> clients = new ConcurrentHashMap<>();
    /**
     * 支付客户端 Class Map
     */
    private final Map<UnifiedPayChannelEnum, Class<? extends PayClient<?>>> clientClass = new ConcurrentHashMap<>();

    public PayClientFactoryImpl() {
        // 微信支付客户端
        clientClass.put(WX_PUB, WxPubPayClient.class);
        clientClass.put(WX_LITE, WxLitePayClient.class);
        clientClass.put(WX_APP, WxAppPayClient.class);
        clientClass.put(WX_BAR, WxBarPayClient.class);
        clientClass.put(WX_NATIVE, WxNativePayClient.class);
        clientClass.put(WX_WAP, WxWapPayClient.class);
        // 支付包支付客户端
        clientClass.put(ALIPAY_WAP, AlipayWapPayClient.class);
        clientClass.put(ALIPAY_QR, AlipayQrPayClient.class);
        clientClass.put(ALIPAY_APP, AlipayAppPayClient.class);
        clientClass.put(ALIPAY_PC, AlipayPcPayClient.class);
        clientClass.put(ALIPAY_BAR, AlipayBarPayClient.class);
        // 钱包支付客户端
        clientClass.put(WALLET, WalletPayClient.class);
        // 模拟支付客户端
        clientClass.put(MOCK, MockPayClient.class);
        // 现金支付客户端
        clientClass.put(CASH, CashPayClient.class);
        // 免支付客户端
        clientClass.put(FREE, FreePayClient.class);
        // MallBook 聚合支付客户端
        clientClass.put(MALLBOOK_WX_LITE, MallBookWxLitePayClient.class);
    }

    @Override
    public void registerPayClientClass(UnifiedPayChannelEnum channel, Class<? extends PayClient<?>> payClientClass) {
        clientClass.put(channel, payClientClass);
    }

    @Override
    @SuppressWarnings("unchecked")
    public <Config extends PayClientConfig, Client extends AbstractPayClient<Config>> Client createOrUpdatePayClient(String channelCode, Config channelConfig) {
        AbstractPayClient<Config> client = (AbstractPayClient<Config>) clients.get(channelCode);
        if (client == null) {
            client = this.createPayClient(channelCode, channelConfig);
            client.init();
            clients.put(channelCode, client);
        } else {
            client.refresh(channelConfig);
        }
        Assert.notNull(client, String.format("获取支付客户端(%s) 失败", channelCode));
        return (Client) client;
    }

    @SuppressWarnings("unchecked")
    private <Config extends PayClientConfig> AbstractPayClient<Config> createPayClient(String channelCode, Config config) {
        UnifiedPayChannelEnum channelEnum = UnifiedPayChannelEnum.getByCode(channelCode);
        Assert.notNull(channelEnum, String.format("支付渠道(%s) 为空", channelCode));
        Class<?> payClientClass = clientClass.get(channelEnum);
        Assert.notNull(payClientClass, String.format("支付渠道(%s) Class 为空", channelCode));
        log.debug("[createPayClient][创建支付客户端({})] {}", channelCode, payClientClass.getName());
        return (AbstractPayClient<Config>) ReflectUtil.newInstance(payClientClass, config);
    }

}
